﻿using UnityEngine;
using System.Collections;
using System.Collections.Generic;

namespace PathologicalGames
{
	public static class SpawnPoolExtension
	{
		/// <summary>
		/// 获取池中的name的对象 如果没有 则调用load代理读取 创建 然后再次获取
		/// </summary>
		/// <param name="self"></param>
		/// <param name="name"></param>
		/// <param name="load"></param>
		/// <returns></returns>
		public static Transform SpawnOrCreate(this SpawnPool self,string name,System.Func<string,Transform> load,bool activing = true)
		{
			Transform ins = null;
			if(self.prefabs.TryGetValue(name,out ins))
			{
			}
			else
			{
				var trans = load(name);
				if(trans != null)
				{
					var copy = trans;//GameObject.Instantiate<Transform>(trans);
					copy.name = name;
					var pl = new PrefabPool(copy);
					copy.gameObject.SetActive(false);
					copy.SetParent(self.transform,false);
					self.CreatePrefabPool(pl);
					ins = copy;
				}
			}
			if(ins != null)
			{
				ins = self.Spawn(ins);
				ins.gameObject.SetActive(activing);
			}
			return ins;
		}
	}
}
